#!/bin/sh

echo 'Detecting OS...'
if [ `uname` = Linux ]; then 
 echo '  Linux detected, >>using -lrt<<'
 echo 'SHMFLAGS=-lrt' > Makefile
 SHMFLAGS='-lrt'
else
 echo ' ' `uname` 'detected, defaulting to >>NOT using -lrt<<'
 echo 'SHMFLAGS=' > Makefile
 SHMFLAGS=
fi
echo


OPTFLAGS=
echo 'Detecting if gcc supports -march=native ... '
TMPFILE=`mktemp /tmp/configure.sh.tmp.XXXXX`
echo 'int main(){return 0;}'|gcc -x c -march=native -o $TMPFILE - 2>/dev/null
if [ $? -ne 0 ]; then
 echo '  This gcc does >>NOT support -march=native<<'
else
 echo '  This gcc >>supports -march=native<<'
 OPTFLAGS="$OPTFLAGS -march=native"
fi
rm -f $TMPFILE
echo


TMPFILE=`mktemp /tmp/configure.sh.tmp.XXXXX`
echo 'Detecting if gcc supports --combine..'
gcc -Wall -O3 --combine -fwhole-program $OPTFLAGS $SHMFLAGS tntesi.c tnvisit.c tngraph.c tnmethods.c -o $TMPFILE 2>/dev/null
if [ $? -ne 0 ]; then
 echo '  gcc is too old or broken(eg. ubuntu), and does >>NOT compile with --combine<<'
else
 echo '  gcc new enough and not broken, >>will use --combine<<'
 OPTFLAGS="--combine -fwhole-program $OPTFLAGS"
fi
rm -f $TMPFILE
echo


echo 'Detecting if the system supports MAP_HUGETLB (Linux 2.6.32 and above)...'
TMPFILE=`mktemp /tmp/configure.sh.tmp.XXXXX`
printf '#include <sys/types.h>\n#include <sys/stat.h>\n#include<fcntl.h>\n#include <stdlib.h>\n#include <sys/mman.h>\nint main(){int fd;void*mm;fd=open("'"$TMPFILE"'",O_RDONLY);mm=map(NULL,256,PROT_READ,MAP_SHARED|MAP_HUGETLB,fd,0);if(mm==MAP_FAILED)return 1;return 0;}\n'|gcc -x c -o $TMPFILE - 2>/dev/null
if [ $? -ne 0 ]; then
 echo '  System is too old and does >>NOT support MAP_HUGETLB<<'
else
 if $TMPFILE; then
 echo '  >>MAP_HUGETLB support ENABLED<<'
 OPTFLAGS="$OPTFLAGS -DMMAP_HAS_HUGETLB"
 else
 echo '  The kernel is probably too old. >>MAP_HUGETLB support DISABLED<<' 
 fi
fi
rm -f $TMPFILE
echo

echo "OPTFLAGS=$OPTFLAGS" >>Makefile

echo 'Detecting presence of /dev/random..'
if [ -c /dev/random ]; then 
 DEFINES="$DEFINES -DHAS_DEVRANDOM"
 echo '  /dev/random support added.'
fi

if [ "$DEBUG_OUTPUT" = 'stdout' ] ; then 
 echo "Debug output will be sent to stdout."
 DEFINES="$DEFINES -DERR=stdout"
fi

echo "DEFINES=$DEFINES" >>Makefile

cat Makefile.in >> Makefile

